#include <iostream>
#include <fstream>
using namespace std;


class Complex
{
    private:
        double real;
        double imag;

    public:
        Complex(double r, double i) : real(r), imag(i) {}

        // 运算符+重载
        Complex operator+(const Complex &other) const
        {
            double newReal = real + other.real;
            double newImag = imag + other.imag;
            return Complex(newReal, newImag);
        }


        // 运算符*重载
        Complex operator * (const Complex & other) const
        {
            return Complex((real * other.real),(imag * other.imag));
        }

        // ++i
        Complex operator ++ ()
        {
            ++(this->real);
            ++(this->imag);
            return *this;
        }

        // i++

        Complex operator ++ (int)
        {
            Complex cpx = *this;
            (this->real)++;
            (this->imag)++;
            return cpx;

        }


        // 运算符<<重载，用于输出对象
        friend std::ostream & operator << (std::ostream &os, const Complex &c);
        // 运算符>>重载，用于输入对象
        friend std::istream & operator >> (std::istream &is, Complex &c);
        // 运算符-重载,用于执行对象的-
        friend Complex operator - (const Complex & a, const Complex & b);
        // 运算符>重载,用于比较对象
        friend bool operator > (const Complex & a, const Complex & b);
        // 运算符<重载,用于比较对象
        friend bool operator < (const Complex & a, const Complex & b);
        // 运算符==重载,用于比较对象
        friend bool operator == (const Complex & a, const Complex & b);
        // 赋值运算符重载，只能重载为类的成员函数
        Complex & operator = (const Complex & other)
        {

            this->real = other.real;
            this->imag = other.imag;
            return *this;
        }
        // 类型转换函数重载，只能重载为类的成员函数
        operator string()
        {
            return static_cast<string>("Complex");
        }
        operator int()
        {
            return static_cast<int>(real);
        }
        operator char()
        {
            return static_cast<char>(real);
        }
        
};

// 输出运算符重载
std::ostream & operator<<(std::ostream &os, const Complex &c)
{
    os << "(" << c.real << ", " << c.imag << ")";
    return os;
}

// 输入运算符重载
std::istream & operator >> (std::istream &is, Complex &c)
{
    std::cout << "Enter real: ";
    std::cin >> c.real;
    std::cout << "Enter imag: ";
    std::cin >> c.imag;
    return is;
}

// 重载运算符-为友元函数
Complex operator - (const Complex & a, const Complex & b)
{
    return Complex((a.real-b.real),(a.imag-b.imag));
}

// 关系运算符>重载
bool operator > (const Complex & a, const Complex & b)
{
    if((a.real > b.real) && (a.imag > b.imag))
    {
        return true;
    }
    else
    {
        return false;
    }
}
// 关系运算符<重载
bool operator < (const Complex & a, const Complex & b)
{
    if((a.real < b.real) && (a.imag < b.imag))
    {
        return true;
    }
    else
    {
        return false;
    }
}
// 关系运算符==重载
bool operator == (const Complex & a, const Complex & b)
{
    if((a.real == b.real) && (a.imag == b.imag))
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    
    

    {   Complex a(1.0, 2.0);
        Complex b(3.0, 4.0);
        std::cout << "*************单目运算符重载i++*************" << std::endl;
        std::cout << "a: " << a << " a++: " << a++ << std::endl;
        std::cout << "a: " << a << " (a++): " << (a++) << std::endl;

        std::cout << "*************单目运算符重载++i*************" << std::endl;
        
        std::cout << "a: " << a << " ++a: " << ++a << std::endl;
        std::cout << "a: " << a << " (++a): " << (++a) << std::endl;
    }
    {
        Complex a(1.0, 2.0);
        Complex b(3.0, 4.0);
        std::cout << "*************双目运算符重载++i*************" << std::endl;
        Complex c = a + b; // 使用运算符+进行复杂数相加

        std::cout << "a+b: " << c << std::endl;

        

        Complex f = a * b;

        std::cout << "a*b: " << f << std::endl;



        std::cout << "*************全局函数为友元函数****************" << std::endl;

        Complex d = a - b;

        std::cout << "a-b: " << d << std::endl;

        std::cout << "*************输入运算符重载****************" << std::endl;

        Complex hu(0.0,0.0);

        std::cin >> hu;
        std::cout << "*************输出运算符重载****************" << std::endl;
        cout << hu << endl;

        std::cout << "*************关系运算符重载****************" << std::endl;
        Complex hu1(3.4,5.6);
        Complex hu2(2.1,2.3);
        cout << "hu1: " << hu1 << " hu2: " << hu2 << endl;
        cout << "hu1 > hu2: " << (hu1 > hu2) << endl;
        cout << "hu1 < hu2: " << (hu1 < hu2) << endl;
        cout << "hu1 == hu2: " << (hu1 == hu2) << endl;

        std::cout << "*************赋值运算符重载****************" << std::endl;
        Complex hu3(3.4,5.6);
        Complex hu4(2.1,2.3);
        cout << "[赋值前] " << "hu3: " << hu3 << " hu4: " << hu4 << endl;
        hu4 = hu3;
        cout << "[赋值后] " << "hu3: " << hu3 << " hu4: " << hu4 << endl;

        std::cout << "*************类型转换运算符重载****************" << std::endl;

        Complex hu5(65.4,5.6);
        string str_name = hu5;
        cout << "类名to string: " << str_name << endl; 

        int int_name = hu5;
        cout << "类名to int: " << int_name << endl;

        char char_name = hu5;
        cout << "类名to int: " << char_name << endl;  

    }
    
    


    return 0;
}


